package com.kll.tv.views.fragment.main

import android.graphics.Color
import android.view.View
import android.widget.Toast
import com.kll.carolinehmibiz.OperationManager
import com.kll.carolinehmibiz.model.AccountModel
import com.kll.carolinehmibiz.model.OperateListener
import com.kll.carolinehmibiz.net.bean.RefreshTokenResponse
import com.kll.carolinehmibiz.presentation.listener.MainContactServiceProtocol
import com.kll.carolinehmibiz.presentation.presenter.MainContactServicePresenter
import com.kll.tv.base.views.MainBaseFragment
import com.kll.tv.databinding.FragmentMainContactserviceBinding
import com.kll.tv.utils.AppToast
import com.kll.tv.utils.TUICallingService
import com.tencent.liteav.trtccalling.model.TUICalling
import com.tencent.liteav.trtccalling.model.impl.TUICallingManager
import com.tencent.liteav.trtccalling.model.util.TUICallingConstants
import com.tencent.qcloud.tuicore.util.ToastUtil

/**
 * ====================================================
 * Created by Ziliang Cai on 2021/12/7 22:19
 * <a href="mailto:752777456@qq.com">Contact me</a>
 * ====================================================
 */
class MainContactServiceFragmengt: MainBaseFragment<MainContactServicePresenter>(), MainContactServiceProtocol.IView {

    val SDKAPPID = 1400374451
    var mCallName: String? = null
    var mCallType: TUICalling.Type?= null

    private lateinit var binding: FragmentMainContactserviceBinding

    override fun createPresenter(): MainContactServicePresenter {
        return MainContactServicePresenter(requireContext(), this)
    }

    override fun initViewBinding(): View {
        binding = FragmentMainContactserviceBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun init() {

        TUICallingService.getInstance(requireContext()).login()

        binding.ivVoice.setOnClickListener {
            it.setFocusable(true)
            it.requestFocus()
            it.setFocusableInTouchMode(true)
            it.requestFocusFromTouch()

            mCallName = TUICallingConstants.TYPE_AUDIO
            mCallType =   TUICalling.Type.AUDIO
            mPresenter.getAssignCustomerService()
        }

        binding.ivVideo.setOnClickListener {
            it.setFocusable(true)
            it.requestFocus()
            it.setFocusableInTouchMode(true)
            it.requestFocusFromTouch()

            mCallName = TUICallingConstants.TYPE_VIDEO
            mCallType =   TUICalling.Type.VIDEO
            mPresenter.getAssignCustomerService()
        }

        binding.ivVoice.setOnFocusChangeListener { v, hasFocus ->
            if (hasFocus) {
                binding.tvVoice.setTextColor(Color.parseColor("#008399"))
            } else {
                binding.tvVoice.setTextColor(Color.parseColor("#B5B5B6"))
            }
        }

        binding.ivVideo.setOnFocusChangeListener { v, hasFocus ->
            if (hasFocus) {
                binding.tvVideo.setTextColor(Color.parseColor("#008399"))
            } else {
                binding.tvVideo.setTextColor(Color.parseColor("#B5B5B6"))
            }
        }

    }

    override fun getRoomIdSuccess(tecentUserId: String,roomid: String) {
        if(TUICallingService.getInstance(requireContext()).isLogin()) {
            val userIDs = arrayOf(tecentUserId)
//            val bundle = Bundle()
//            bundle.putString(TUICallingConstants.PARAM_NAME_TYPE, mCallName)
//            bundle.putStringArray(TUICallingConstants.PARAM_NAME_USERIDS, userIDs)
//            bundle.putString(TUICallingConstants.PARAM_NAME_GROUPID, "")
            TUICallingManager.sharedInstance().call(userIDs, roomid,mCallType)
        } else {
            ToastUtil.toastLongMessage("分配客服失败，请重试！")
        }
    }

    override fun getRoomIdFailed(code: Int, message: String) {
        AppToast.show(message, 2000)
        if (code == 102) {
            // token过期 刷新token
            OperationManager.getInstancce().refreshToken(object : OperateListener {
                override fun onSuccess(state: Int, obj: Any?) {
                    var data = obj as RefreshTokenResponse
                    AccountModel.getInstance(context!!).updateUserToken(data.token)
                }

                override fun onFail(code: Int, message: String) {
                    AppToast.show(message, 2000)
                    if (code == 101) {
                        loginAgain()
                    }
                }

            })
        }
        if (code == 101) {
            loginAgain()
        }
    }
    override fun assignCSSuccessed(accountNo: String) {
        mPresenter.getRoomId(accountNo)
    }

    override fun showAssignCSLoading() {
        Toast.makeText(requireContext(), "获取中...", Toast.LENGTH_SHORT).show()
    }

    override fun assignCSFailed(code: Int, message: String) {
        Toast.makeText(requireContext(), message, Toast.LENGTH_LONG).show()
        if (code == 102) {
            // token过期 刷新token
            OperationManager.getInstancce().refreshToken(object : OperateListener {
                override fun onSuccess(state: Int, obj: Any?) {
                    var data = obj as RefreshTokenResponse
                    AccountModel.getInstance(context!!).updateUserToken(data.token)
                    mPresenter.getAssignCustomerService()
                }

                override fun onFail(code: Int, message: String) {
                    AppToast.show(message, 2000)
                    if (code == 101) {
                        loginAgain()
                    }
                }

            })
        }
        if (code == 101) {
            loginAgain()
        }
    }
}